;+
;PURPOSE
;	to drive SLUG with a variety of inputs
;SYNTAX
;	drive_slug, model_name=model_name, [, timestep=timestep, 
;	 maxtime=maxtime, galaxy_sfr=galaxy_sfr,
;        imf_type=imf_type, 
;	  pathtoout=pathtoout]
;INPUTS
;All inputs must be put in as an array of strings
;	timestep: [default '2E6']
;	maxtime:  [default '1E8']
;	log_time: [default '0']
;	clust_frac: [default '1.0]
;	clust_disrupt: [default '1.0']
;	galaxy_sfr: [default '1']
;	imf_type:   [defualt 'Kroupa']
;	imf_slope:   [defualt '-2.35']
;	libpath: [default 'padagb/020/pau_smi/maedr']
;	galaxy_sfh: [default 'mysfh.txt']
;       max_cluster_mass: [default '1E7']
;       min_cluster_mass: [default '20']]
;	slope_cmf: [default '-2']
;	drawtype: [default 'RANDOM']
;	seed: [default 0]
;	fluxes: [default 'FUV,QH0']
;	colors: [default 'FUV-QH0']
;	binflux:[default '2']
;	bincolor:[default '2']
;	minflux:[default '1']
;	maxflux:[default '50']
;	mincolor:[default '1']
;	maxcolor:[default '20']
;	outcluster:[default 'no']
;	imfout:[default 'no']
;	log:name of log file
;KEYWORDS
;	pathtoout: path to output directory 
;		[default to '$SLUG_DIR/output/']
;	fits_input: set to a string if you want to write
;		out the idl structure used to direct
;		this program (for future use)
;	in: if you have written an input file via fits_input in
;		the past then you can simply pass that structure to this
;		program to run it again
;	_extra: keywords to pass to read_slug
;	/dry: set to only create the param file
;Written by R. da Silva, UCSC, 6-14-10
;Updated to new param. MF, Feb 2011
;Updated some input stuff, RdS, 3-3-11
;-

pro drive_slug, timestep=timestep, maxtime=maxtime, log_time=log_time,$
		 galaxy_sfr=galaxy_sfr,$
                imf_type=imf_type, libpath=libpath, model_name=model_name, $
                galaxy_sfh=galaxy_sfh, max_cluster_mass=max_cluster_mass, $
                min_cluster_mass=min_cluster_mass, slope_cmf=slope_cmf,$
                clust_frac=clust_frac, drawtype=drawtype, seed=seed, $
                fluxes=fluxes, colors=colors, binflux=binflux, bincolor=bincolor, $
                maxcolor=maxcolor, mincolor=mincolor, maxflux=maxflux, minflux=minflux,$
                pathtoout=pathtoout, in=in, fits_input=fits_input, outcluster=outcluster,$
                imf_slope=imf_slope,dry=dry,log=log,$
                noclobber=noclobber, imfout=imfout, _extra=_extra, outimf=outimf, alias=alias,$
		emptylog=emptylog, clust_disrupt=clust_disrupt
;n_run=max(n_elements(timestep), n_elements(maxtime), n_elements(galaxy_sfr),$
;	n_elements(imf_type), n_elements(metallicity), n_elements(model_name))

;;set a bunch of defaults
if keyword_set(outimf) then  imfout=outimf
if not keyword_set(alias) then  alias='slug '

;if not keyword_set(log) then log='$SLUG_DIR/logs/'+model_name+'.log'
;;journal, log
if not keyword_set(in) then begin
   if  keyword_set(pathtoout) then outdir=pathtoout+'/output/'
   if not keyword_set(pathtoout) then begin
      pathtoout='$SLUG_DIR/output/'
      outdir=pathtoout
   endif   
   if not keyword_set(timestep) then timestep='1E6'
   if not keyword_set(maxtime) then maxtime='1E8'
   if not keyword_set(log_time) then log_time='0'
   if not keyword_set(galaxy_sfr) then galaxy_sfr='1'
   if not keyword_set(imf_type) then imf_type='KROUPA'
   if not keyword_set(libpath) then libpath='iso/padagb/020/lej+smi/maedr'
   if not keyword_set(galaxy_sfh) then galaxy_sfh='mysfh.txt'
   if not keyword_set(max_cluster_mass) then max_cluster_mass='1E7'
   if not keyword_set(min_cluster_mass) then min_cluster_mass='20'
   if not keyword_set(slope_cmf) then slope_cmf='-2'
   if not keyword_set(imf_slope) then imf_slope='-2.35'
   if not keyword_set(clust_frac) then clust_frac='1.0'
   if not keyword_set(clust_disrupt) then clust_disrupt='1.0'
   if not keyword_set(drawtype) then drawtype='RANDOM'   
   if not keyword_set(seed) then seed=0
   if not keyword_set(fluxes) then fluxes='FUV,QH0'
   if not keyword_set(colors) then colors='FUV-QH0'
   if not keyword_set(binflux) then binflux='2'
   if not keyword_set(bincolor) then bincolor='2'
   if not keyword_set(maxcolor) then maxcolor='20'
   if not keyword_set(mincolor) then mincolor='1'
   if not keyword_set(maxflux) then maxflux='50'
   if not keyword_set(minflux) then minflux='1'
   if not keyword_set(outcluster) then outcluster='no'
   if size(outcluster, /type) NE 7 then outcluster=(outcluster EQ 0?'no':'yes')
   if not keyword_set(imfout) then imfout='no'
   if size(imfout, /type) NE 7 then imfout=(imfout EQ 0?'no':'yes')
   if not keyword_set(model_name) then begin
      ;doc, 'drive_slug'
      splog, 'Model_name **MUST** be set'
      return
   endif
   
   ;;create array for model 
   n_run=n_elements(model_name)
   in=replicate({timestep:'', maxtime:'', log_time:'',$
		galaxy_sfr:'', imf_type:'',imf_slope:'', $
                 libpath:'', model_name:'',galaxy_sfh:'', max_cluster_mass:'', $
                 min_cluster_mass:'', slope_cmf:'',clust_frac:'', $
		 clust_disrupt:'', drawtype:'',$
		 seed:'',$
                 fluxes:'', colors:'', binflux:'', bincolor:'', $
                 maxcolor:'', mincolor:'', maxflux:'', minflux:'',outcluster:'', imfout:''}, n_run)
   in.timestep=timestep
   in.maxtime=maxtime
   in.log_time=log_time
   in.galaxy_sfr=galaxy_sfr
   in.imf_type=imf_type
   in.imf_slope=imf_slope
   in.libpath=libpath
   in.model_name=model_name
   in.galaxy_sfh=galaxy_sfh
   in.max_cluster_mass=max_cluster_mass
   in.min_cluster_mass=min_cluster_mass
   in.slope_cmf=slope_cmf 
   in.clust_frac=clust_frac
   in.clust_disrupt=clust_disrupt
   in.drawtype=drawtype
   in.seed=seed
   in.fluxes=fluxes 
   in.colors=colors
   in.binflux=binflux
   in.bincolor=bincolor
   in.maxcolor=maxcolor
   in.mincolor=mincolor
   in.maxflux=maxflux
   in.minflux=minflux
   in.outcluster=outcluster
   in.imfout=imfout
   
   ;;write structure 
   if keyword_set(fits_input) then mwrfits, in, fits_input, /create
endif


;;run the models
for i=0L, n_run-1 do begin

   ;;create the par file
   openw, lun, pathtoout+in[i].model_name+'.param', /get_lun

   printf, lun, '####'
   printf, lun, '#SLUG PARAMETER FILE'
   printf, lun, '####'
   printf, lun, '#Length of time steps (yr)'
   printf, lun, 'timestep                 '+in[i].timestep+ ' #'
   printf, lun, '#Maximum evolution age (yr) [<1E9]'
   printf, lun, 'maxtime                  '+in[i].maxtime+ ' #'
   printf, lun, '#Enable logarithmic timestep [0 or time in Myr of first timestep]'
   printf, lun, 'log_time                  '+in[i].log_time+ ' #'
   printf, lun, '#Star formation rate (Msun/yr) [-1 for SFH]'
   printf, lun, 'galaxy_sfr               '+in[i].galaxy_sfr+' #'
   printf, lun, '#SFH file (time[yr], sfr[M/yr]) [set -1 in SFR]'
   printf, lun, 'galaxy_sfh               '+in[i].galaxy_sfh+' #'
   printf, lun, '#IMF [KROUPA, CHABRIER, SALPETER, IGIMF]'
   printf, lun, 'imf_type                 '+in[i].imf_type+' #'
   printf, lun, '#IMF_SLOPE if choose SLOPE for IMF, this sets the slope'
   printf, lun, 'imf_slope                '+in[i].imf_slope+' #'
   printf, lun, '#Path to stellar library [padagb/020/pau_smi/maedr]'
   printf, lun, 'libpath              '+in[i].libpath+' #'
   printf, lun, '#Name of the model'
   printf, lun, 'model_name               '+in[i].model_name+' #'
   printf, lun, '#Maximum cluster mass [or if you select IGIMF, this is the time interval in years to average the SFR over to determine the maximum cluster mass]'
   printf, lun, 'max_cluster_mass                 '+in[i].max_cluster_mass+' #'
   printf, lun, '#Minimum cluster mass'
   printf, lun, 'min_cluster_mass                 '+in[i].min_cluster_mass+' #'
   printf, lun, '#Slope of the cluster mass function'
   printf, lun, 'slope_cmf                '+in[i].slope_cmf+' #'
   printf, lun, '#Fraction of clustered stars [0-1a]'
   printf, lun, 'clust_frac                '+in[i].clust_frac+' #'
   printf, lun, '#Cluster disruption parameters, 0 for no disruption [0-1]'
   printf, lun, 'clust_disrupt                     '+in[i].clust_disrupt+' #'
   printf, lun, '#Switch between random/sequential drawing [RANDOM,SEQUENIAL]'
   printf, lun, 'drawtype                 '+in[i].drawtype+' #'
   printf, lun, '#Seed to use in the random sequence [0 uses time as seed]'
   printf, lun, 'seed                 '+in[i].seed+' #'
   printf, lun, '#Fluxes to compute (comma separated) [NUV,FUV,BOL,QH0,J,H,K,SDSSu,SDSSg,SDSSr,SDSSi,SDSSz,johnsonU,johnsonB,johnsonV,johnsonR,johnsonI]'
   printf, lun, 'fluxes                   '+in[i].fluxes+' #'
   printf, lun, '#Colors to compute from the above fluxes (comma separated) [A-C,B-C]'
   printf, lun, 'colors                   '+in[i].colors+' #'
   printf, lun, '#Number of bin in flux to sample (single integer)'
   printf, lun, 'binflux                  '+in[i].binflux+' #'
   printf, lun, '#Number of bin in color to sample (single integer)'
   printf, lun, 'bincolor                  '+in[i].bincolor+' #'
   printf, lun, '#Maximum vales for colors, matching the above request (comma separated)'
   printf, lun, 'maxcolor                  '+in[i].maxcolor+' #'
   printf, lun, '#Minimum vales for colors, matching the above request (comma separated)'
   printf, lun, 'mincolor                  '+in[i].mincolor+' #'
   printf, lun, '#Maximum vales for fluxes, matching the above color request (comma separated)'
   printf, lun, 'maxflux                   '+in[i].maxflux+' #'
   printf, lun, '#Minimum vales for fluxes, matching the above color request (comma separated)'
   printf, lun, 'minflux                   '+in[i].minflux+' #'
   printf, lun, '#Output the full cluster file? [yes,no]'
   printf, lun, 'outcluster                '+in[i].outcluster+' #'
   printf, lun, '#Output the full IMF distribution file? [yes,no]'
   printf, lun, 'imfout                '+in[i].imfout+' #'
   close, lun
   
   
   ;;now run
   ;splog, 'Working on model ', i+1, ' of ', n_run
   close, /all
if keyword_set(dry) EQ 0 then begin
if strmatch(imfout, 'no') then imfski=1
if strmatch(outcluster, 'no') then skip_clu=1
   
   if keyword_set(noclobber) EQ 0 then begin
	cmd='$SLUG_DIR/'+alias+' '+pathtoout+in[i].model_name+'.param'
	if keyword_set(log) then cmd=cmd+' > '+log
      spawn,cmd
      read_slug, in[i].model_name, path=outdir, _extra=_extra, $
		skip_cluster=skip_clu, imf_skip=imfski
   endif else begin
      if file_test(outdir+in[i].model_name+'.dat') EQ 0 $
      then begin 
	cmd='$SLUG_DIR/'+alias+' '+pathtoout+in[i].model_name+'.param'
	if keyword_set(log) then cmd=cmd+' > '+log
        spawn,cmd
        ;spawn, '$SLUG_DIR/'+alias+' '+pathtoout+in[i].model_name+'.param > '+log
         read_slug, in[i].model_name, path=outdir, _extra=_extra,$
		skip_cluster=skip_clu, imf_skip=imfski
      endif else splog, 'Do not overwrite ', in[i].model_name
   endelse
   
endif
endfor

;;journal
end
